package com.lj.luoye.lotteryticket;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

import androidx.appcompat.app.AppCompatActivity;

import com.lj.luoye.lotteryticket.dao.RegisterDao;
import com.lj.luoye.lotteryticket.dialog.MyDialogUtil;
import com.lj.luoye.lotteryticket.entity.Register;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

public class RegisterActivity extends AppCompatActivity {
    final String registerCode="lejljlf5ljjj2dfe0";
    final int maxTryDays=30;
    final int maxTryCount=10;

    private RegisterDao registerDao;
    private EditText codeEt;
    private Button tryBt;
    private Button registerBt;
    private EditText hrefTv;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_register);

        registerDao=new RegisterDao(getApplicationContext());
        @SuppressLint("SimpleDateFormat") final SimpleDateFormat simpleDateFormat=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Register register=registerDao.getOne();
        if(register==null){
            register=new Register();
            register.date= simpleDateFormat.format(new Date());
            register.startDate="";
            register.code="";
            registerDao.insert(register);
        }
        System.out.println("register->"+register);
        if(register.code.equals(registerCode)){
            startActivity();
            return;
        }else if(register.startDate!=null&&!register.startDate.equals("")){
            try {
                Date startTime=simpleDateFormat.parse(register.startDate);
                System.out.println("startTime->"+startTime);
                System.out.println("new Date()->"+new Date());
                long days=register.tryDays;
                long start=startTime.getTime()+ 1000L * 3600L * 24L * days;
//                long start=startTime.getTime()+ 1000L * days;
                long now=new Date().getTime();
                System.out.println("days->"+days+",start->"+start+",now->"+now);
                if(start>now){//没有超过一个月
                    startActivity();
                    return;
                }
            } catch (ParseException e) {
                e.printStackTrace();
            }

        }

        codeEt=findViewById(R.id.codeEt);
        tryBt=findViewById(R.id.tryBt);
        registerBt=findViewById(R.id.registerBt);
        final Register finalRegister = register;
        tryBt.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(finalRegister.tryCount>maxTryCount){
                    MyDialogUtil.showMessageDialog(RegisterActivity.this,"注意","试用次数超限，请购买注册码！");
                    return;
                }
                String startDate=simpleDateFormat.format(new Date());
                int days=(int) (Math.random()*maxTryDays+1);
                int count=finalRegister.tryCount+1;
                registerDao.updateStart(finalRegister.id,startDate,days,count);
                RegisterActivity.this.startActivity();
            }
        });
        registerBt.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String code=codeEt.getText().toString();
                if(code.equals(registerCode)){
                    registerDao.updateCode(finalRegister.id,code);
                    RegisterActivity.this.startActivity();
                }else{
                    MyDialogUtil.showMessageDialog(RegisterActivity.this,"注意","注册码错误！");
                }
            }
        });



        hrefTv=findViewById(R.id.hrefTv);
        String html="https://shop264863345.taobao.com";//这里即使不加协议好HTTP；也能自动被系统识别出来。
        hrefTv.setText(html);
//        hrefTv.setAutoLinkMask(Linkify.ALL);
//        hrefTv.setMovementMethod(LinkMovementMethod.getInstance());
        hrefTv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                openTaobaoShopping();
            }
        });
    }

    private void startActivity(){
        Intent intent=new Intent(this,MainActivity.class);
        startActivity(intent);
        finish();
    }

    private boolean checkPackage(String packageName) {
        if (packageName == null || "".equals(packageName)) return false;
        try {
            getPackageManager().getApplicationInfo(packageName, PackageManager.GET_UNINSTALLED_PACKAGES);
            return true;
        } catch (PackageManager.NameNotFoundException e) {
            return false;
        }
    }
    private void openTaobaoShopping() {
        if (checkPackage("com.taobao.taobao")) {
            Intent intent = new Intent();
            intent.setAction("android.intent.action.VIEW");
//            https://detail.tmall.com/item.htm?id=535823983028&spm=a21bo.7932663.item.2.0mvUrk&scm=1007.13596.65361.100200300000014
            String url = "taobao://shop264863345.taobao.com";
            Uri uri = Uri.parse(url);
            intent.setData(uri);
            startActivity(intent);
        } else {
            Uri uri = Uri.parse("https://shop264863345.taobao.com");
            Intent intent = new Intent(Intent.ACTION_VIEW, uri);
            startActivity(intent);
        }
    }
}
